﻿using RAP.Framework.Libary.Utils.Collections.Dictionary;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace RAP.Framework.Libary.DomTemplateBuilder.Element.Parameter
{
	/// <summary>
	/// 参数集合对象
	/// </summary>
	public class ParameterCollection: IEnumerable<ParameterValue>
	{
		private IDictionary<string, int> _paramIndex = new Dictionary<string, int>();

		/// <summary>
		/// 
		/// </summary>
		public int CurrentIndex { get; private set; } = 0;

		/// <summary>
		/// 请不要直接更改该对象！！！
		/// </summary>
		protected IList<ParameterValue> Parameters { get; } = new List<ParameterValue>();

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		public void AddParameter( string name, object value )
		{
			if ( _paramIndex.ContainsKey( name ) )
			{
				return;
			}
			lock ( _paramIndex )
			{
				Parameters.Add( new ParameterValue { Name = name, Value = value } );
				_paramIndex.Add( name, CurrentIndex );
				CurrentIndex++;
			}
		}

		public IEnumerator<ParameterValue> GetEnumerator( )
		{
			return Parameters.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator( )
		{
			return Parameters.GetEnumerator();
		}

		/// <summary>
		/// 根据名称获取参数对象
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public ParameterValue this[ string name ]
		{
			get
			{
				if ( _paramIndex.TryGetValue( name, out int idx ) )
				{
					return Parameters[ idx ];
				}
				return null;
			}
		}
	}
}
